package web;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DaoUtil{
	public static void main(String[] args) throws ClassNotFoundException, SQLException {
		LoginQuery lq=new LoginQuery();
		lq.uid=123;
		lq.pword="mypassword";
		Boolean data=DaoUtil.getDaoUtil().query(lq);
		System.out.println(data);

	//	List<Map> data=DaoUtil.getDaoUtil().query(new TopicQuery());
	//	for(Map row : data){
	//		System.out.println(row.get("theme"));
	//	}
	}
	
	
	private DaoUtil(){}
	static DaoUtil du=null;
	static DaoUtil getDaoUtil(){
		if(du==null){
			du=new DaoUtil();
		}
		return du;
	}
	
	
	
	Connection conn=null;
	Statement stmt=null;
	ResultSet rs=null;
	void conn() throws ClassNotFoundException, SQLException{
		Class.forName("com.mysql.jdbc.Driver");
		conn=DriverManager.getConnection("jdbc:mysql://localhost/test?user=root&password=root");
		stmt=conn.createStatement();
	}
	<T> T query(QueryItem<T> qi) throws SQLException, ClassNotFoundException{
		try{
			conn();
			rs=stmt.executeQuery(qi.getSql());
			return qi.run(rs);
		}catch(SQLException e){
			throw e;
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			throw e;
		}finally{
			close();
		}
	}
	void close(){
		if(conn!=null){
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			if(stmt!=null){
				try {
					stmt.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				if(rs!=null){
					try {
						rs.close();
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		}
	}
}

abstract class QueryItem<T>{
	abstract public String getSql();
	abstract public T run(ResultSet rs) throws SQLException;
}
class TopicQuery extends QueryItem<List<Map>>{

	@Override
	public String getSql() {
		return "select * from topic";
	}

	@Override
	public List<Map> run(ResultSet rs) throws SQLException {
		List<Map> res=new ArrayList();
		while(rs.next()){
			Map row=new HashMap();
			row.put("theme",rs.getString("theme"));
			res.add(row);
		}
		return res;
	}
	
}

class LoginQuery extends QueryItem<Boolean>{

	int uid=3;
	String pword="mypassword";
	@Override
	public String getSql() {
		
		return "select * from users where uid="+uid+" and pword='"+pword+"';";
	}

	@Override
	public Boolean run(ResultSet rs) throws SQLException {
		while(rs.next()){
			return true;
		}
		return false;
	
	}
	
	
	
}



//public abstract class BaseDao {
//	Connection conn=null;
//	Statement stmt=null;
//	public BaseDao() throws ClassNotFoundException, SQLException{
//		try {
//			conn();
//		} catch (ClassNotFoundException e){
//			throw e;
//		} catch (SQLException e){
//			throw e;
//		}
//	}
//	void conn() throws ClassNotFoundException, SQLException{
//		Class.forName("com.mysql.jdbc.Driver");
//		conn=DriverManager.getConnection("jdbc:mysql://localhost/test?user=root&password=root");
//		stmt=conn.createStatement();
//		
//	}
//	void close(){
//		if(conn!=null){
//			try {
//				conn.close();
//			} catch (SQLException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
//			if(stmt!=null){
//				try {
//					stmt.close();
//				} catch (SQLException e) {
//					// TODO Auto-generated catch block
//					e.printStackTrace();
//				}
//			}
//		}
//	}
//	public static void main(String[] args) {
//		try {
//			TopicQuery tq=new TopicQuery();
//			for (Map row : tq.res) {
//				System.out.println(row.get("theme"));
//			}
//			
//		} catch (ClassNotFoundException | SQLException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//	}
//}
//
//class QueryDao extends BaseDao{
//	ResultSet rs;
//
//	public QueryDao(QueryItem qi) throws ClassNotFoundException, SQLException {
//		super();
//		try{
//			rs=stmt.executeQuery(qi.getSql());
//			qi.run(rs);
//		}catch(SQLException e){
//			throw e;
//		}finally{
//			close();
//		}
//	}
//	@Override
//	void close() {
//		super.close();
//		if(rs!=null){
//			try {
//				rs.close();
//			} catch (SQLException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
//		}
//	}
//}
//
//abstract class QueryItem{
//	public QueryItem() throws ClassNotFoundException, SQLException{
//		new QueryDao(this);
//	}
//	public List<Map> res=new ArrayList();
//	abstract public String getSql();
//	abstract void run(ResultSet rs) throws SQLException;
//}
//
//class TopicQuery extends QueryItem{
//	public TopicQuery() throws ClassNotFoundException, SQLException {
//		super();
//		// TODO Auto-generated constructor stub
//	}
//	@Override
//	public String getSql(){
//		return "select * from topic";
//	}
//	@Override
//	void run(ResultSet rs) throws SQLException {
//		while(rs.next()){
//			Map row=new HashMap();
//			row.put("theme",rs.getString("theme"));
//			res.add(row);
//		}
//	}
//}
//
//


